package com.suian.spring.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAnnotationAspect {
    private static final Logger logger = LoggerFactory.getLogger(LogAnnotationAspect.class);

    //定义切入点
    @Pointcut("@annotation(com.suian.spring.annotation.Loggable)")
    public void logAbleMethods() {

    }

    @Before("logAbleMethods()")
    public void beforeAdvice(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        logger.info("注解日志--》方法{}开始执行",methodName);
    }

    @After("logAbleMethods()")
    public void afterAdvice(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        logger.info("注解日志--》方法{}执行完成",methodName);
    }

    // 记录正常返回结果
    @AfterReturning(pointcut = "logAbleMethods()", returning = "result")
    public void afterReturningAdvice(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        logger.info("注解日志--》方法 [{}] 正常返回，结果: {}", methodName, result);
    }
}
